
using System;

namespace Sedna.Domain.Uniqueness
{
    public interface IKey
    {
        Type Type { get; }
        Object Value { get; }
    }

    public class Key : IKey
    {
        private Type type;
        private Object value;


        private Key(Type type, bool validate)
        {
            if (type == null)
                throw new ArgumentNullException("Type cannot be null");

            if (validate && type.IsValueType)
                throw new ArgumentException(String.Format("Value of type '{0}' cannot be null", type));

            this.type = type;
        }

        public Key(Type type) : this(type, true)
        { }

        public Key(Type type, object value) : this(type, false)
        {
            if (value == null)
            {
                if (type.IsValueType)
                    throw new ArgumentException(String.Format("Value of type '{0}' cannot be null", type));
            }
            else if (!value.GetType().Equals(type))
            {
                throw new ArgumentException(String.Format("Type of value '{0}' doesn't match to '{1}'", value.GetType(), type));
            }

            this.value = value;
        }


        public Type Type
        {
            get { return type; }
        }

        public Object Value
        {
            get { return value; }
        }
    }
}
